package com.zzptc.lzh.myappone.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zzptc.lzh.myappone.R;
import com.zzptc.lzh.myappone.databinding.RecipeItemBinding;
import com.zzptc.lzh.myappone.entity.ResultsBean;

import java.util.List;

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder>{
    private List<ResultsBean>recipesList;
    private Context context;

    public MyRecyclerViewAdapter(List<ResultsBean> recipesList, Context context) {
        this.recipesList = recipesList;
        this.context = context;
    }

    @NonNull
    @Override//小页面
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecipeItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
                R.layout.recipe_item, parent, false);//RecipeItemBinding代表整个小页面
        return new ViewHolder(binding);
    }

    @Override//绑定数据
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //将数据拿出来
        ResultsBean resultsBean=recipesList.get(position);
        holder.recipeItemBinding.tvCaiming.setText(resultsBean.getTitle());//获取菜名
        Glide.with(context)//获取图片
                .load(resultsBean.getCover())
                .into(holder.recipeItemBinding.ivTupian);
    }

    @Override//返回长度
    public int getItemCount() {
        return recipesList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private RecipeItemBinding recipeItemBinding;
        public ViewHolder(@NonNull RecipeItemBinding recipeItemBinding) {
            super(recipeItemBinding.getRoot());//调用父类方法
            this.recipeItemBinding=recipeItemBinding;

        }
    }
}
